package com.Sdebruijn.curriculumvitae.asynctask;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.Sdebruijn.curriculumvitae.R;
import com.Sdebruijn.curriculumvitae.db.DatabaseManager;
import com.Sdebruijn.curriculumvitae.listeners.OnDoneLoadingListener;
import com.Sdebruijn.curriculumvitae.models.App;
import com.Sdebruijn.curriculumvitae.util.ViewUtils;
import com.Sdebruijn.curriculumvitae.view.ProgressWheel;
import com.Sdebruijn.curriculumvitae.view.RemoteImageView;

/**
 * Loads the data of a Person's Apps in a separate background thread with
 * {@link AsyncTask}
 * 
 * @author Stefan de Bruijn
 */
// TODO Eventually replace this with a service - per example with RoboSpice
public class LoadAppDataAsyncTask extends AsyncTask<Void, Void, List<App>> {

	private OnDoneLoadingListener mOnDoneLoadingListener;

	/*
	 * Data fields from the fragment, in order according to the view
	 */
	private LinearLayout ll_apps_data;
	private RemoteImageView riv_app_block_logo;
	private TextView tv_app_block_name, tv_app_block_quick_description, tv_app_block_link;

	private ProgressWheel pg;

	private LayoutInflater inflater;

	public LoadAppDataAsyncTask(final View layout, final Activity activity,
			final OnDoneLoadingListener listener) {
		this.mOnDoneLoadingListener = listener;

		inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		ll_apps_data = (LinearLayout) layout.findViewById(R.id.ll_apps_data);
		pg = (ProgressWheel) layout.findViewById(R.id.pw_spinner);

	}

	@Override
	protected void onPreExecute() {
		// Show the progress wheel and make it spin
		ViewUtils.switchVisibleState(ll_apps_data, ViewUtils.State.INVISIBLE, pg,
				ViewUtils.State.VISIBLE);
		pg.spin();
	}

	@Override
	protected List<App> doInBackground(Void... params) {
		return DatabaseManager.getInstance().getAllApps();
	}

	@Override
	protected void onPostExecute(List<App> results) {

		for (App app : results) {

			View vAppBlock = inflater.inflate(R.layout.merge_app_block_shaded, null);

			tv_app_block_link = (TextView) vAppBlock.findViewById(R.id.tv_app_block_link);
			riv_app_block_logo = (RemoteImageView) vAppBlock.findViewById(R.id.riv_app_block_logo);
			tv_app_block_name = (TextView) vAppBlock.findViewById(R.id.tv_app_block_name);
			tv_app_block_quick_description = (TextView) vAppBlock
					.findViewById(R.id.tv_app_block_quick_description);

			tv_app_block_link.setText(app.package_name);

			riv_app_block_logo.setRemotePath(app.icon);
			riv_app_block_logo.loadImage(true);

			tv_app_block_name.setText(app.name);
			tv_app_block_quick_description.setText(app.description);

			ll_apps_data.addView(vAppBlock);

		}

		mOnDoneLoadingListener.onDoneLoading(ll_apps_data);

		ViewUtils.switchVisibleState(ll_apps_data, ViewUtils.State.VISIBLE, pg,
				ViewUtils.State.INVISIBLE);

		pg.stopSpinning();
	}
}
